<template>
  <div>
    <el-card class="box-construction" shadow="always">
      <div class="box-title box-title-position">
        <div>管理员设置</div>
        <el-button class="add-btn" size="medium" type="primary" @click="addManage()">新增管理员</el-button>
      </div>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        style="width: 100%;margin-top:25px"
      >
        <el-table-column :show-overflow-tooltip="true" prop="id" label="ID" width="50"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="realname" label="管理员"></el-table-column>
        <el-table-column prop="version" label="操作" width="100">
          <template slot-scope="scope">
            <el-button size="small" icon="el-icon-delete" @click="delManage(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="box-page"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pagination.currentPage"
        :page-sizes="[5,10, 20, 30, 40]"
        :page-size="pagination.pageSize"
        layout="total,sizes, prev, pager, next"
        :total="pagination.total"
      ></el-pagination>
    </el-card>
    <ManageForm
      ref="form"
      :title="formTitle"
      :datas.sync="editRow"
      :visible.sync="visible"
      :baseStyle="baseStyle"
      @saveForm="submit"
    />
  </div>
</template>

<script>
import manageAPI from "@apis/setManage";
import ManageForm from "./editManageForm/index";
export default {
  data() {
    return {
      loading: false,
      searchVal1: "",
      tableData: [],
      editRow: {},
      visible: false,
      baseStyle: {
        width: "140px"
      },
      formTitle: "新增管理员",
      pagination: {
        currentPage: 1,
        pageNo: 1,
        pageSize: 5,
        total: 0
      }
    };
  },
  components: {
    ManageForm
  },
  created() {
    this.loading = true;
    this.getList(1);
  },
  mounted() {},
  methods: {
    getList(val = 1) {
      let parmars = {
        pageNo: val,
        pageSize: this.pagination.pageSize
      };
      manageAPI
        .getManageList(parmars)
        .then(res => {
          this.pagination = {
            currentPage: res.current_page,
            pageNo: res.current_page,
            pageSize: res.per_page,
            total: res.total
          };
          this.tableData = [...res.data];
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          this.$message.error(err.message);
        });
    },
    //控制每页条数
    handleSizeChange(val) {
      // console.log(`每页${val}条`);
      this.pagination.pageSize = val;
      this.loading = true;
      this.getList(this.pagination.pageNo);
    },
    //分页
    handleCurrentChange(val) {
      // console.log(`当前第${val}页`);
      this.loading = true;
      this.getList(val);
    },
    addManage() {
      this.formTitle = "新增管理员";
      this.visible = true;
      this.editRow = {};
    },
    delManage(data) {
      this.$confirm("删除管理员, 是否继续?", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (this.pagination.pageNo > 1 && this.tableData.length == 1) {
            this.pagination.pageNo = this.pagination.pageNo - 1;
          }
          this.loading = true;
          manageAPI
            .delManageUser(data.uid)
            .then(res => {
              this.getList(this.pagination.pageNo);
            })
            .catch(err => {
              this.$message.error(err.message);
            });
        })
        .catch(() => {});
    },
    submit(val) {
      // console.log(val,"val");
      manageAPI
        .addManageUser({ uid: val.id })
        .then(res => {
          this.$refs.form.cancel();
          this.loading = true;
          this.getList(this.pagination.pageNo);
        })
        .catch(err => this.$message.error(err.message));
    }
  }
};
</script>

<style lang="scss" scoped>
.box-title-position {
  position: relative;
  .add-btn {
    position: absolute;
    right: 0;
    top: -3px;
  }
}
.box-construction {
  min-height: 100px;
  .box-page {
    text-align: right;
    padding-top: 40px;
  }
}
</style>